package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3LiveScanFinderView extends View {

    //相机遮罩框外面的线，阴影区域，滚动线
    private Paint area, line;
    //相机遮罩框中间透明区域
    private Rect center;
    //屏幕大小
    private int screenHeight, screenWidth;

    private int lineX = 0;
    //滚动线速度
    private static final int SPEED = 5;

    private int textTop;

    private int centerLeft;

    private Context mContext;
    private int centerTop;

    public X3LiveScanFinderView(Context context) {
        super(context);
    }

    public X3LiveScanFinderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        area = new Paint(Paint.ANTI_ALIAS_FLAG);
        area.setStyle(Paint.Style.FILL);
        area.setColor(Color.parseColor("#000000"));
        area.setAlpha(40);

        screenHeight = X3ScreenUtils.getScreenHeight(context);
        screenWidth = X3ScreenUtils.getScreenWidth(context);
        center = getCenterRect(context, 234, 234);

        line = new Paint(Paint.ANTI_ALIAS_FLAG);
        line.setStyle(Paint.Style.FILL);
        //line.setColor(Color.parseColor("#86FFE1"));

    }

    public X3LiveScanFinderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 根据尺寸获取中间区大小
     *
     * @param context
     * @param height
     * @param width
     * @return
     */
    private Rect getCenterRect(Context context, int height, int width) {
        height = X3ScreenUtils.dipToPx(height,context);
        width = X3ScreenUtils.dipToPx(width,context);
        Rect rect = new Rect();
        centerLeft = (this.screenWidth - width) / 2;
        centerTop = (this.screenHeight - height) / 2;
        rect.set(centerLeft, centerTop, centerLeft + width, centerTop + height);
        return rect;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制四周阴影区域（上下左右），注意+1和-1，不设置不显示边框
        canvas.drawRect(0, 0, screenWidth, center.top - 1, area);
        canvas.drawRect(0, center.bottom + 1, screenWidth, screenHeight, area);
        canvas.drawRect(0, center.top - 1, center.left - 1, center.bottom + 1, area);
        canvas.drawRect(center.right + 1, center.top - 1, screenWidth, center.bottom + 1, area);

        //滚动线
        lineX += SPEED;
        if (center.top + lineX >= center.bottom)
        {
            lineX = SPEED;
        }
        LinearGradient linearGradient = new LinearGradient(center.left,center.top,center.left ,center.top + lineX,
                new int[]{Color.parseColor("#10ffffff"),Color.parseColor("#8086FFE1")},
                new float[]{0.5f,1f},Shader.TileMode.MIRROR);
        line.setShader(linearGradient);
        canvas.drawRect(center.left,center.top,center.right,center.top + lineX,line);

        postInvalidate();
    }

}